package com.example.studentsRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.Dormitory;
import com.example.studentsRoom.entity.Student;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.mapper.DormitoryMapper;
import com.example.studentsRoom.mapper.StudentMapper;
import com.example.studentsRoom.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentsRoom.util.CommonUtil;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.StudentVo;
import com.example.studentsRoom.vo.TransitionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@Service
public class StudentService extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    DormitoryMapper dormitoryMapper;

    @Override
    public Boolean saveStudent(Student student) {
        //添加学生信息
        student.setStuDate(CommonUtil.creatDate());
        int insert = studentMapper.insert(student);
        if(insert!=1){
            return false;
        }
        //修改宿舍剩余床位
        Dormitory dormitory = dormitoryMapper.selectById(student.getDorId());
        if (Integer.parseInt(dormitory.getDorBed())==0){
            return false;
        }
        dormitory.setDorBed(String.valueOf(Integer.parseInt(dormitory.getDorBed())-1));
        int i = dormitoryMapper.updateById(dormitory);
        if (i!=1){
            return false;
        }
        return true;
    }

    /**
     * 分压查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageVo listPage(Integer page, Integer size) {
        Page<Student> studentPage=new Page<>(page,size);

        Page<Student> resultPage = this.studentMapper.selectPage(studentPage, null);

        PageVo pageVo = TransitionVo.transition(resultPage, dormitoryMapper);
        return pageVo;
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @Override
    public PageVo search(SearchForm searchForm) {
        //模糊查询加分页
        Page<Student> studentPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Student> resultPage = null;

        //判断value值是不是null
        if (searchForm.getValue().equals("")){
            resultPage = this.studentMapper.selectPage(studentPage, null);
        }else{
            QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),searchForm.getValue());
            resultPage = this.studentMapper.selectPage(studentPage, queryWrapper);
        }

        PageVo pageVo = TransitionVo.transition(resultPage, dormitoryMapper);
        return pageVo;
    }

    /**
     * 学生信息更新
     * @param studentVo
     * @return
     */
    @Override
    public Boolean update(StudentVo studentVo) {

        Student student=new Student();
        BeanUtils.copyProperties(studentVo,student);

        //更新学生信息
        int update = studentMapper.updateById(student);
        if (update!=1){
            return false;
        }
        //更新宿舍数量
        if (!studentVo.getOldDorId().equals(student.getDorId())){//如果新老id不一样
            //old+1,new-1
            try {
                dormitoryMapper.addAvailable(studentVo.getDorId());
                dormitoryMapper.subAvailable(studentVo.getOldDorId());
            }catch (Exception e){
                System.out.println(e);
                return false;
            }
        }

        student.getDorId();
        return true;
    }

    public Boolean deleteById(String id){
        //修改宿舍数据
        Student student=studentMapper.selectById(id);
        try {
            dormitoryMapper.addAvailable(student.getDorId());
        }catch (Exception e){
            return false;
        }
        //删除学生数据
        int delete = studentMapper.deleteById(id);
        if (delete!=1){
            return false;
        }
        return true;
    }
}
